package aaaAddr;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.openqa.jetty.http.SSORealm;

import aaaCfg.IocX4addr;

import com.attilax.db.DBX;
import com.attilax.io.filex;
import com.attilax.io.pathx;
import com.attilax.json.AtiJson;
import com.attilax.lang.text.strUtil;
import com.attilax.order.absService;
import com.attilax.time.timeUtil;
import com.attilax.user.AbsService;
import com.attilax.web.ReqX;


/**
 * aaaAddr.AddrSvr
 * @author Administrator
 *
 */
public class AddrSvr extends AbsService {

	public AddrSvr() {
		if (this.dbx == null)
			dbx = IocX4addr.getBean(DBX.class);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
 	AddrSvr ac = IocX4addr.getBean(AddrSvr.class);
ac. 	testDb();
//		String s = ac.insert(new HashMap() {
//			{
//				this.put("textarea", "陈生,13652566665,广东省 广州市 荔湾区 详细地址,5163");
//
//			}
//		});
//		System.out.println(s);
//		System.out.println("---f");

	}
	@SuppressWarnings("all")
	public String submitOk(Object reqx) {
		try {
			Map req = null;

			if (reqx instanceof HttpServletRequest)
				req = ReqX.toMap((HttpServletRequest) reqx);

			String sql = "";
			sql = "select * from exp where uname='@u@' order by id desc limit 1";
			sql = sql.replace("@u@", req.get("uname").toString());
			
		
			Map m = this.dbx.uniqueResult(sql);
			String lastBatchId = m.get("批次号").toString();

			String sql2 = "update exp set 完成状态='已完成'   where    uname='@u@' and   批次号='@bid@' ";
			sql2 = sql2.replace("@u@", req.get("uname").toString());
			sql2 = sql2.replace("@bid@", lastBatchId);
			int r = this.dbx.execSql_retInt(sql2);
		

			return String.valueOf(r);
		} catch (Exception e) {
			return AtiJson.toJson(e);
		}
	}
public String testDb()
{
	
	String sql=" select * from exp limit 20 ";
	String s=AtiJson.toJson( this.dbx.findBySql(sql));
	System.out.println(s);return s ;
}
	public String insert(Object reqx) {
		try {
			Map req = null;

			if (reqx instanceof HttpServletRequest)
				req = ReqX.toMap((HttpServletRequest) reqx);

			String frm = (String) req.get("from");
			frm = strUtil.replaceCnChar(frm).trim();
			String[] frm_a = frm.split(",");
			String s = (String) req.get("textarea");
			String[] a = s.split("\r\n");
			String batid = filex.getUUidName();
			for (String line : a) {
				try {
					if (line.trim().length() == 0)
						continue;
					String[] item_a = line.split(",");
					String name = item_a[0];
					String add = item_a[2];
					String sql = "insert into exp ( toP , toA,fromP,fromA ,typex,uname,timex)values('@u@','@add@','@fp@','@fa@','@tp@','@uid@')";
					sql = "update exp set toP='@u@' , toA='@add@',fromP='@fp@',fromA='@fa@' ,uname='@uid@',timex='@tm@',批次号='@bid@' where ( uname is null or uname='') and typex='@tp@'  limit 1 ";
					sql = sql.replace("@u@", name);
					sql = sql.replace("@add@", add);
					sql = sql.replace("@uid@", req.get("uname").toString());
					sql = sql.replace("@fp@", frm_a[0]);
					sql = sql.replace("@fa@", frm_a[2]);
					sql = sql.replace("@tp@", (String) req.get("type_id"));
					sql = sql.replace("@tm@", timeUtil.Now_CST());
					
					sql = sql.replace("@bid@",batid);
					System.out.println(sql);

					this.dbx.execSql_retInt(sql);
				} catch (Exception e) {
					e.printStackTrace();
					filex.saveLog(e, pathx.webAppPath() + "/logsQ1");
					 
				}

			}
			return "ok";
		} catch (Exception e) {
			return AtiJson.toJson(e);
		}

	}

}
